package com.tfjybj.itoo.teach.provider.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dmsdbj.itoo.tool.base.entity.YearSemesterEntity;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.ElectiveCoursePubEntity;
import com.tfjybj.itoo.teach.model.CourseModel;
import com.tfjybj.itoo.teach.model.ElectiveCoursePubModel;
import com.tfjybj.itoo.teach.provider.service.CourseService;
import com.tfjybj.itoo.teach.provider.service.ElectiveCoursePubService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * ElectiveCoursePubController
 * electiveCoursePub表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"公选课培养计划接口"})
@RequestMapping(value = "/electiveCoursePub")
@RestController
public class ElectiveCoursePubController {

    @Resource
    private ElectiveCoursePubService electiveCoursePubService;

    @Resource
    private CourseService courseService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model ElectiveCoursePubModel
     * @return 添加的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody ElectiveCoursePubModel model) {
        if (StringUtils.isEmpty(model.getSemesterId())) {
            return ItooResult.build(ItooResult.FAIL, "学年学期不能为空");
        }
        if (StringUtils.isEmpty(model.getAcademyId())) {
            return ItooResult.build(ItooResult.FAIL, "学院不能为空");
        }
        if (StringUtils.isEmpty(model.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "课程不能为空");
        }
        if (StringUtils.isEmpty(model.getExaminationFormId())) {
            return ItooResult.build(ItooResult.FAIL, "考核形式不能为空");
        }
        ElectiveCoursePubEntity electiveCoursePubEntity = new ElectiveCoursePubEntity();
        BeanUtils.copyProperties(model, electiveCoursePubEntity);
        electiveCoursePubEntity.setId(IdWorker.getIdStr());
        electiveCoursePubEntity.setIsArrangement(0);
        //判断公选课培养计划是否已经存在
        Boolean idIsExist = electiveCoursePubService.findIdIsExist(electiveCoursePubEntity.getSemesterId(), electiveCoursePubEntity.getCourseId());
        if (idIsExist = true){
            return ItooResult.build(ItooResult.FAIL, "该培养计划已存在，不能重复添加！");
        }
        electiveCoursePubService.save(electiveCoursePubEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        electiveCoursePubService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        electiveCoursePubService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model ElectiveCoursePubModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改公选课培养计划")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody ElectiveCoursePubModel model) {
        if (StringUtils.isEmpty(model.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "课程为空");
        }
        ElectiveCoursePubEntity electiveCoursePubEntity = new ElectiveCoursePubEntity();
        BeanUtils.copyProperties(model, electiveCoursePubEntity);
        //判断公选课培养计划是否已经存在
        Boolean idIsExist = electiveCoursePubService.findIdIsExist(electiveCoursePubEntity.getSemesterId(), electiveCoursePubEntity.getCourseId());
        if (idIsExist = true){
            return ItooResult.build(ItooResult.FAIL, "该培养计划已存在,请重新编辑！");
        }
        electiveCoursePubService.updateById(electiveCoursePubEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找ElectiveCoursePub
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        ElectiveCoursePubEntity electiveCoursePubEntity = electiveCoursePubService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", electiveCoursePubEntity);
    }

    /**
     * 分页查询所有ElectiveCoursePub
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "分页查询所有培养计划——逆向生成")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<ElectiveCoursePubEntity> electiveCoursePubs = electiveCoursePubService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", electiveCoursePubs);
    }

    /**
     * 根据课程_id模糊查询electiveCoursePub
     *
     * @param courseId 课程_id
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的electiveCoursePub
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据课程_id模糊查询electiveCoursePub", notes = "分页根据课程_id模糊查询electiveCoursePub")
    @GetMapping(value = "/queryElectiveCoursePubByLikeCourseId/{pageNo}/{pageSize}")
    public ItooResult queryElectiveCoursePubByLikeCourseId(@RequestParam(required = false, defaultValue = "") String courseId,
                                                           @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                                           @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<ElectiveCoursePubEntity> electiveCoursePubList = electiveCoursePubService.queryByLikeCourseId(courseId, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", electiveCoursePubList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据课程ID和学年ID查询公选课培养计划
     *
     * @param courseId   课程ID
     * @param semesterId 学年ID
     * @return 公选课培养计划实体集合
     * @author 李建帅
     * @since 2.0.0 2018/11/14 15:39
     */
    @ApiOperation(value = "根据课程ID和学年ID查询公选课培养计划")
    @GetMapping(value = "/queryByCoSeId/{courseId}/{semesterId}")
    public ItooResult queryByCoSeId(@ApiParam(name = "courseId", value = "课程ID", required = true) @PathVariable String courseId,
                                    @ApiParam(name = "semesterId", value = "学期ID", required = true) @PathVariable String semesterId) {
        List<ElectiveCoursePubModel> electiveCoursePubModels = electiveCoursePubService.queryByCoSeId(courseId, semesterId);
        return ItooResult.build(ItooResult.SUCCESS, "查询公选课培养计划成功!", electiveCoursePubModels);
    }

    /**
     * 根据学年学期ID分页查询公选课培养计划
     *
     * @param semesterId 学年学期ID
     * @param pageNo     页码
     * @param pageSize   每页条数
     * @return 公选课培养计划实体集合
     * @author 李建帅
     * @since 2.0.0 2018-11-15 10:39:08
     */
    @ApiOperation(value = "根据学年学期ID分页查询公选课培养计划")
    @GetMapping(value = "/queryAllBySemesterId/{semesterId}/{pageNo}/{pageSize}")
    public ItooResult queryAllBySemesterId(@ApiParam(name = "semesterId", value = "学年学期ID", required = true) @PathVariable String semesterId,
                                           @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                           @ApiParam(name = "pageSize", value = "每页条数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<ElectiveCoursePubModel> electiveCoursePubModelList = electiveCoursePubService.queryAllBySemesterId(semesterId, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询公选课培养计划成功!", electiveCoursePubModelList);
    }

    /**
     * 默认分页查询所有的公选课培养计划
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return 公选课培养计划实体集合
     * @author 李建帅
     * @since 2.0.0 2018-11-15 11:25:36
     */
    @ApiOperation(value = "默认查询所有的公选课培养计划")
    @GetMapping(value = "/queryAllEcpPlan/{pageNo}/{pageSize}")
    public ItooResult queryAllEcpPlan(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                      @ApiParam(name = "pageSize", value = "每页条数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<ElectiveCoursePubModel> electiveCoursePubModelPageInfo = electiveCoursePubService.queryAllEcpPlan(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询公选课培养计划成功!", electiveCoursePubModelPageInfo);
    }

    /**
     * 根据公选课培养计划ID查询公选课培养计划详情
     *
     * @param idList 公选课培养计划ID
     * @return 公选课培养计划实体集合
     * @author 李建帅
     * @since 2.0.0 2018/11/15 19:11
     */
    @ApiOperation(value = "根据公选课培养计划ID查询公选课培养计划详情")
    @GetMapping(value = "/queryEcpPlanById/{idList}")
    public ItooResult queryEcpPlanById(@ApiParam(name = "id", value = "公选课培养计划ID", required = true) @PathVariable List<String> idList) {
        List<ElectiveCoursePubModel> electiveCoursePubModels = electiveCoursePubService.queryEcpPlanById(idList);
        return ItooResult.build(ItooResult.SUCCESS, "查询公选课培养计划成功!", electiveCoursePubModels);
    }

    /**
     * 查询所有的学年学期
     *
     * @return 学年学期实体集合
     * @author 李建帅
     * @since 2.0.0 2018/11/15 21:18
     */
    @ApiOperation(value = "查询所有的学期")
    @GetMapping(value = "/querySemester")
    public ItooResult querySemester() {
        List<YearSemesterEntity> yearSemesterEntities = electiveCoursePubService.querySemester();
        return ItooResult.build(ItooResult.SUCCESS, "查询学期成功！", yearSemesterEntities);
    }

    /**
     * 根据课程名称模糊查询公选课培养计划
     *
     * @param strLike 模糊查询条件（课程名称）
     * @return 公选课培养计划实体集合
     * @author 李建帅
     * @since 2.0.0 2018/11/19 9:15
     */
    @ApiOperation(value = "根据课程名称模糊查询公选课培养计划")
    @GetMapping(value = "/queryByStrLike/{pageNo}/{pageSize}")
    public ItooResult queryByStrLike(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                     @ApiParam(name = "pageSize", value = "每页条数", required = true, example = "10") @PathVariable Integer pageSize,
                                     @ApiParam(name = "strLike", value = "模糊查询条件（课程名称）") @RequestParam(required = false, defaultValue = "") String strLike) {
        PageInfo<ElectiveCoursePubModel> electiveCoursePubModelList = electiveCoursePubService.queryByStrLike(pageNo, pageSize,strLike);
        return ItooResult.build(ItooResult.SUCCESS, "查询公选课培养计划信息成功！", electiveCoursePubModelList);
    }

    /**
     * 根据条件查询公选课培养计划
     *
     * @param semesterId 学期ID
     * @param academyId  学院Id
     * @return 公选课培养计划实体集合
     * @author 李建帅
     * @since 2.0.0 2018-11-19 10:28:18
     */
    @ApiOperation(value = "根据条件查询公选课培养计划")
    @GetMapping(value = "/queryBySemAcaId/{pageNo}/{pageSize}")
    public ItooResult queryBySemAcaId(@ApiParam(name = "semesterId", value = "学年ID") @RequestParam(required = false, defaultValue = "") String semesterId,
                                      @ApiParam(name = "academyId", value = "学院ID") @RequestParam(required = false, defaultValue = "") String academyId,
                                      @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                      @ApiParam(name = "pageSize", value = "每页码数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<ElectiveCoursePubModel> electiveCoursePubModelPageInfo = electiveCoursePubService.queryBySemAcaId(semesterId, academyId, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询公选课培养计划成功！", electiveCoursePubModelPageInfo);
    }

    /**
     * 根据学院ID查询公选课
     *
     * @param academy 学院id
     * @return coursemodel
     * @author 李建帅
     * @since 2018-11-19 15:51:00
     */
    @ApiOperation(value = "根据学院ID查询公选课信息")
    @GetMapping(value = "/queryPubCourseByAcademy/{academy}")
    public ItooResult queryPubCourseByAcademy(@ApiParam(name = "academy", value = "学院ID", required = true) @PathVariable String academy) {
        List<CourseModel> courseModels = courseService.queryPubCourseByAcademy(academy);
        return ItooResult.build(ItooResult.SUCCESS, "查询公选课信息成功！", courseModels);
    }

    /**
     * 根据学年ID和课程id判断该培养计划是否已经存在
     *
     * @param semesterId 学年Id
     * @param courseId   课程Id
     * @return boolean 存在true/不存在false
     * @author 李建帅
     * @since 2.0.0 2018/11/24 15:29
     */
    @ApiOperation(value = "根据学年ID和课程id判断该培养计划是否已经存在")
    @GetMapping(value = "/findIdIsExist/{semesterId}/{courseId}")
    public ItooResult findIdIsExist(@ApiParam(name = "semesterId", value = "学年ID", required = true) @PathVariable String semesterId,
                                    @ApiParam(name = "courseId", value = "课程ID", required = true) @PathVariable String courseId) {
        Boolean idIsExist = electiveCoursePubService.findIdIsExist(semesterId, courseId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", idIsExist);
    }

    /**
     * 下载公选选课培养计划模板，批量导入使用
     *
     * @param response 响应
     * @return 下载模板是否成功
     * @author 李建帅
     * @since 2.0.0 2018-11-15 08:53:53
     */
    @ApiOperation(value = "下载公选课培养计划模板，批量导入使用", notes = "下载公选课培养计划模板，批量导入使用")
    @GetMapping(value = {"/downLoadTemplate"})
    public ItooResult downLoadTemplate(HttpServletResponse response) {
        if (electiveCoursePubService.downLoadTemplate(response)) {
            return ItooResult.build(ItooResult.SUCCESS, "下载培养计划模板成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "下载培养计划模板失败");
        }
    }

    /**
     * 批量导入公选课培养计划
     *
     * @param multipartFile 批量导入的文件
     * @param request       请求
     * @param response      响应
     * @return ItooResult 批量导入的结果
     * @author 李建帅
     * @since 2.0.0 2018-11-18 09:05:59
     */
    @ApiOperation(value = "批量导入公选课培养计划", notes = "0001:部分成功；0000:成功；1111:失败")
    @PostMapping(value = {"/importByTemplate"})
    public ItooResult importByTemplate(@RequestParam(value = "file") MultipartFile multipartFile,
                                       HttpServletRequest request, HttpServletResponse response) {
        return electiveCoursePubService.importTemplate(multipartFile, request, response);
    }

    /**
     * 根据ID导出培养计划
     *
     * @param response 响应
     * @param idList   培养计划ID
     * @return 导出结果信息
     * @author 李建帅
     * @since 2.0.0 2018-11-21 21:08:12
     */
    @ApiOperation(value = "根据ID导出培养计划")
    @GetMapping(value = {"/exportAll"})
    public ItooResult exportAll(HttpServletResponse response, @RequestParam(required = false, defaultValue = "") List<String> idList) {
        return electiveCoursePubService.exportByTpId(response, idList);
    }

    /**
     * 根据条件导出培养计划
     *
     * @param response   响应
     * @param academyId  学院Id
     * @param semesterId 学期ID
     * @param strLike    模糊条件
     * @return 导出结果信息
     * @author 李建帅
     * @since 2.0.0 2018-11-25 11:34:25
     */
    @ApiOperation(value = "根据条件导出培养计划")
    @GetMapping(value = {"/exportByStrLike"})
    public ItooResult exportByStrLike(HttpServletResponse response, @RequestParam(required = false, defaultValue = "") String academyId, @RequestParam(required = false, defaultValue = "") String semesterId, @RequestParam(required = false, defaultValue = "") String strLike) {
        return electiveCoursePubService.exportByStrLike(response, academyId, semesterId, strLike);
    }

    /**
     * 导出-导入失败的数据
     *
     * @param response  响应
     * @param errorListId 为成功导入的分组id
     * @return 课程信息model
     * @author 李建帅
     * @since 2018-12-2 08:10:56
     */
    @ApiOperation(value = "导出-导入失败的数据")
    @GetMapping(value = "exportFile/downErrorList/{errorListId}")
    public ItooResult downErrorList(HttpServletResponse response,
                                    @ApiParam(name = "errorListId", value = "errorListId", required = true) @PathVariable String errorListId) {

        if (electiveCoursePubService.downErrorList(errorListId,response)){
            return ItooResult.build(ItooResult.SUCCESS,"导出成功");
        } else {
            return ItooResult.build(ItooResult.FAIL,"导出失败");
        }
    }
}
